package com.youan.serve.controller;

import com.youan.pangu.base.bean.ElementTree;
import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.controller.swagger.OrgControllerSwagger;
import com.youan.serve.service.IOrgService;
import com.youan.serve.dao.domain.Org;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Controller
@RequestMapping("/org")
public class OrgController implements OrgControllerSwagger{
    @Autowired
    private IOrgService orgService;

    @Override
    @PostMapping("/org")
    public Org addOrg(Org org, LoginInfor loginInfor){
        org.setOrgId(UUID.randomUUID().toString().replaceAll("-", ""));
        org.setOperator(loginInfor.getUsername());
        org.setOperatedTime(new Date());
        orgService.save(org);
        return null;
    }

    @Override
    @PutMapping("/org")
    public Org updateOrg(Org org, LoginInfor loginInfor){
        org.setOperator(loginInfor.getUsername());
        org.setOperatedTime(new Date());
        orgService.updateById(org);
        return null;
    }

    @Override
    @GetMapping("/orgs")
    public List<ElementTree> selectAllOrg() throws Exception{
        return orgService.getOrgs();
    }

    @Override
    @DeleteMapping("/org/{orgId}")
    public void deleteOrgByOrgId(@PathVariable String orgId) throws Exception{
        orgService.deleteOrg(orgId);
    }

    @Override
    @GetMapping("/org/{orgId}")
    public Org selectOrgByOrgId(@PathVariable String orgId){
        return orgService.getById(orgId);
    }
}

